var request = require("request");
   var base_url = "http://120.25.90.245:8090/utoo"; //测试

var util = {
  getJson: function(ops, req, res, callback) {
    var options, headers = {
      "version": "2.8.0",
      "type": "0",
      "platform": "ios"
    };
    options = {
      method: req.method.toString(),
      json: true
    };
    ops.params = req.app.locals._.assign(options, ops.params);
    ops.params.url = base_url + ops.params.url;
    ops.params.headers = req.app.locals._.assign(headers, ops.headers, {
      "user-token": global.token
    })
    if(ops.params.url != (base_url + "/direct/travel/sync")) {
      console.log(ops);
    }
    request(ops.params, function(error, response, body) {
      if(ops.params.url != (base_url + "/direct/travel/sync")) {
        console.log(body)
      }
      if(callback) {
        callback(error, response, body)
      } else {
        if(!body) {
          res.json({
            "code": 500,
            "msg": "后台未响应"
          });
          return;
        };
        res.json(body);
      }
    });
  },
  dateFormat: function(time, fmt) {
    if(!time) {
      return "";
    }
    fmt = fmt || "yyyy-MM-dd hh:mm:ss";
    var d = new Date(time);
    var o = {
      "M+": d.getMonth() + 1, //月份   
      "d+": d.getDate(), //日   
      "h+": d.getHours(), //小时   
      "m+": d.getMinutes(), //分   
      "s+": d.getSeconds(), //秒   
      "q+": Math.floor((d.getMonth() + 3) / 3), //季度   
      "S": d.getMilliseconds() //毫秒   
    };
    if(/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
    for(var k in o)
      if(new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
    ƒ
  },
  nonceStr: function() {
    //生成8位随机数
    var nonceStr = '';
    var arr = [];
    arr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    for(var i = 0; i < 8; i++) {
      nonceStr += arr[parseInt(Math.random() * 61)];
    }
    return nonceStr
  }
};

module.exports = util;